using System;
using System.Windows.Forms;

using FreeFlow;
using FreeFlow.Administration;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for StageAdaptor.
	/// </summary>
	public class StageAdaptor : Adaptor, IRefreshable
	{
    private Stage stage;
		public StageAdaptor(Stage stage)
		{
      this.stage = stage;
		}

    public override int ImageIndex
    {
      get
      {
        // images for different stage types
        switch (stage.StageType)
        {
          case StageType.User : return 0;
          case StageType.Group : return 5;
          case StageType.System : return 16;
          case StageType.Archive : return 17;
          case StageType.MapSegment : return 18;
          case StageType.SubProcedure : return 2;
          case StageType.Rules : return 28;
          default : throw new FreeFlowException("Unrecognized stage type - " + stage.StageType);
        }
      }
    }

    public override void AddChildren(System.Windows.Forms.TreeNode node)
    {
      // folders
      if (stage.Folders.Count > 0)
      {
        TreeNode foldersNode = TreeNodeFactory.CreateTreeNode(new FoldersAdaptor(stage.Folders));
        node.Nodes.Add(foldersNode);
      }

      // actions
      if (stage.StageType != StageType.Archive)
      {
        TreeNode actionsNode = TreeNodeFactory.CreateTreeNode(new ActionsAdaptor(stage.Actions));
        node.Nodes.Add(actionsNode);
      }
    }

    public override string Text
    {
      get
      {
        return stage.Name + " (" + stage.Folders.Count + " folders)";
      }
    }

    public override Control GetRenderer()
    {
      StageControl control = new StageControl();
      control.LoadStage(stage);
      return control;
    }

    public override object Object
    {
      get
      {
        return stage;
      }
    }

    #region IRefreshable Members

    public void Refresh()
    {
      stage.Refresh();
    }

    #endregion
  }
}
